/* $Header: diag.c,v 2.2 01/10/02 10:26:10 przybyls Exp $ */

/***********************************************************************/
/*                                                                     */
/*                  Copyright Hewlett-Packard Co. 1991                 */
/*                                                                     */
/***********************************************************************/

/***********************************************************************/
/* MODULE NAME:  diag.c                                                */
/* ============                                                        */
/*                                                                     */
/* Author:  Lesley Freed                                               */
/*                                                                     */
/* MODULE PURPOSE:  To provide the main functions for handling         */
/*                  diagnostics execution.                             */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/***********************************************************************/


/*****************************/
/*   GLOBAL INCLUDE FILES    */
/*****************************/

#include <typedefs.h>
#include <error.h>
#include <p_wkfile.h>
#include <wkfile.h>
#include <ramps.h>
#include <zone_config.h>
#include <config.h>
#include <zone_status.h>
#include <p_status.h>
#include <status.h>
#include <i_op.h>
#include <stddef.h>
#include <diag.h>
#include <p_diag.h>

#include <exception.h>
#include <err_handler.h>
#include <p_ui.h>
#include <detector.h>
#include <zone_ui.h>

#include <sig_types.h>
#include <sig_ui.h>

#include <oven.h>

/*****************************/
/*   LOCAL INCLUDE FILES     */
/*****************************/


/*****************************/
/*   GLOBAL VARIABLES        */
/*****************************/

   DIAG_STATE Diag_State;

/*****************************/
/*   EXTERNAL VARIABLES      */
/*****************************/


/*****************************/
/*   LOCAL DEFINES           */
/*****************************/


/*****************************/
/*   STATIC VARIABLES        */
/*****************************/



/*****************************/
/*   IMPLEMENT SECTION       */
/*****************************/

BIT8 GetDiagResource( TEST_TYPE type, DIAG_FROM from )
{
   BIT8 result;

   INTERRUPTS_OFF

   /* If we are not in a run then allow the diagnostic to execute */
   /* if there is not one running already.                        */
   if ( Diag_State.free == TRUE )
   {
      SetNewException( TEST_IN_PROGRESS, 0, 0 );
      Diag_State.free     = FALSE;
      Diag_State.type     = type;
      Diag_State.from     = from;
      Diag_State.status   = BUSY;
      result = TRUE;
   }
   else
   {
      result = FALSE;
   }

   INTERRUPTS_ON

   if ( result )
   {
      Diag_State.result   = PASSED;
      Diag_State.timer    = 0;
      Diag_State.index    = 0;
      Diag_State.counter  = 0;
      Diag_State.counter2 = 0;
      return( TRUE );
   }

   return( FALSE );
}

void FreeIODiagResource( void )
{
   if ( Diag_State.from == FROM_IO )
   {
      FreeDiagResource();
   }
}

void FreeDiagResource( void )
{
   if ( Diag_State.free == FALSE )
   {
      INTERRUPTS_OFF

      Diag_State.from     = FROM_NULL; /* indicate that test is done */
      Diag_State.status   = IDLE;      /* indicate that test is done */
      Diag_State.free     = TRUE;      /* release the diagnostic resource */
      Diag_State.diag_fcn = NULL;      /* there is no pending diag fcn */
      ClearNewException( TEST_IN_PROGRESS );

      INTERRUPTS_ON

   }

}


/***********************************************************************/
/* FUNCTION:  ExitTest                                                 */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  To free diagnostics resources and clear all active test   */
/*           flags.                                                    */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED:  Diag_State,                                          */
/*                Mux_Adc_Time_Response_Test                           */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/

void ExitTest( BIT32 from )
{
   if ( (BIT32)Diag_State.from == from )
   {
      Mux_Adc_Time_Response_Test = FALSE;
      FreeDiagResource();
   }
}


/***********************************************************************/
/* FUNCTION:  HandleDiagnostic                                         */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  To execute any pending diagnostic function.               */
/*                                                                     */
/* RETURNED VALUE:                                                     */
/*                                                                     */
/* GLOBALS USED: Diag_State                                            */
/*                                                                     */
/* CALLS:  function pointed to by Diag_State.diag_fcn                  */
/*                                                                     */
/* LIMITATIONS:  This function assumes that the diagnostic function    */
/*               called is relatively short in duration.  This is      */
/*               because HandleDiagnostic is called in the zone_task.  */
/*                                                                     */
/***********************************************************************/

void HandleDiagnostic( void )
{
   if ( Diag_State.diag_fcn != NULL )       /* is there a pending diagnostic? */
   {
      if ( (*Diag_State.diag_fcn)() == FAILED )
      {
         Diag_State.result = FAILED;
      }
   }

   /* Free the diagnostic resource if we are done and it is not coming from */
   /* IO.  Otherwise IO needs a handshake to know when to read the test     */
   /* results.                                                              */
   if ( ( GetDiagStatus() == DONE ) && ( Diag_State.from != FROM_IO ) )
   {
      FreeDiagResource();
   }

   /* Flag we have evaluated the readiness for diagnostics, a kludge since */
   /* this is not repetatively evaluated, unlike most readiness items.     */
   SetEvalReadyBit( DIAGNOSTICS_MODE );
   SetEvalReadyBit( TEST_IN_PROGRESS );
}

/***********************************************************************/
/* FUNCTION:  ClearDiagnosticSetpts                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  To disable all diagnostic mode setpts.                    */
/*                                                                     */
/* RETURNED VALUE:  none                                               */
/*                                                                     */
/* GLOBALS USED:  many                                                 */
/*                                                                     */
/* CALLS:  many                                                        */
/*                                                                     */
/* LIMITATIONS:   This cannot be guaranteed to be comprehensive.  It   */
/*                is up to the firmware group to make sure all perti-  */
/*                nent setpts are handled.                             */
/*                                                                     */
/***********************************************************************/

void ClearDiagnosticSetpts( void )
{
   INT16 i;

   if ( !IamAdirtball() )
   {
      /* Clear pneumatics setpoints */
      for ( i=0; i<=12; i++ )
      {
         ClearDiagPneuDutyCycle( i );
      }
   }

   SetInletDiagMode( pFRONT_INLET, 0 );
   SetInletDiagMode( pBACK_INLET,  0 );

   /* Clear detector setpoints */
   Det_Diag_Mode = FALSE;
   UpdateDetStatus();

   /* Clear zone setpoints */
   TurnConstDutyCycleOff( pOVEN_TEMP );
   TurnConstDutyCycleOff( pFRONT_INLET_TEMP );
   TurnConstDutyCycleOff( pBACK_INLET_TEMP );
   TurnConstDutyCycleOff( pFRONT_DET_TEMP );
   TurnConstDutyCycleOff( pBACK_DET_TEMP );
   TurnConstDutyCycleOff( pAUX_1_TEMP );
   TurnConstDutyCycleOff( pAUX_2_TEMP );

   TurnCryoConstDutyCycleOff( &Oven_Table );
   TurnCryoConstDutyCycleOff( &Inja_Table );
   TurnCryoConstDutyCycleOff( &Injb_Table );
   TurnCryoConstDutyCycleOff( &Auxa_Table );
   TurnCryoConstDutyCycleOff( &Auxb_Table );

   ClearZoneTuning();

   /* Clear signal setpoints */
   ResetSigPathState();

}

/***********************************************************************/
/* FUNCTION:  SetDiagMode                                              */
/* ========                                                            */
/*                                                                     */
/* PURPOSE:  To set the state of the instrument to diagnostics mode.   */
/*                                                                     */
/* RETURNED VALUE: UI_ERR return code indicating whether or not set    */
/*                 occurred.                                           */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:  none                                                        */
/*                                                                     */
/* LIMITATIONS:  NOTE: sets the diagnostics readiness bit to not ready */
/*                     for ON and clears it for OFF.                   */
/*                                                                     */
/***********************************************************************/

UI_ERR SetDiagMode( ON_OFF on_off )
{
   if ( on_off == ON )
   {
      SetNewException( DIAGNOSTICS_MODE, 0, 0 );
   }
   else
   {
      /* Clear those setpts not cleared by UpdateInstStatus */
      ClearDiagnosticSetpts();

      INTERRUPTS_OFF
         ClearNewException( DIAGNOSTICS_MODE );
      INTERRUPTS_ON
   }

   return( OK );
}

void ClearHacksawSetpts( void )
{
   Actual_Equals_Setpt = (BIT8)OFF;
   Use_Thermal_Setpt   = (BIT8)OFF;
   Hacksaw             = (BIT8)OFF;
}

BIT8 GetDemoMode( void )
{
   if (( Actual_Equals_Setpt == (BIT8)OFF ) &&
       ( Use_Thermal_Setpt   == (BIT8)OFF ) &&
       ( Hacksaw             == (BIT8)OFF ))
   {
      return FALSE;
   }

   return TRUE;
}
